package com.qnzw.zjmy.ad.kaku.nativie;

import android.content.Context;
import android.util.Log;

import com.anythink.core.api.AdError;
import com.anythink.nativead.api.ATNative;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativeNetworkListener;
import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.api.NativeAd;
import com.qnzw.zjmy.ad.impl.AdRevenueListenerImpl;
import com.qnzw.zjmy.home.ui.enums.TaKuAdPositionEnum;

import cn.hutool.core.util.ObjectUtil;

/**
 * 信息流
 */
public class TaKuNativeAdOneCW {
    private static String TAG = "TaKuNativeAdOneCW";
    private static volatile TaKuNativeAdOneCW instance;
    private String token;
    private ATNative atNative;
    private Context context;

    private ATNativeAdView mATNativeAdView; //渲染广告必须创建的容器
    private NativeAd mNativeAd;

    private boolean loadFailFlag = false;

    private TaKuNativeAdOneCW() {
    }

    public static synchronized TaKuNativeAdOneCW getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (TaKuNativeAdOneCW.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new TaKuNativeAdOneCW();
                }
            }
        }
        return instance;
    }

    public void loadAd(Context context, String placementId) {
        this.context = context;
        //初始化广告加载对象
        atNative = new ATNative(context, placementId, new ATNativeNetworkListener() {
            @Override
            public void onNativeAdLoaded() {
                if (!loadFailFlag) {
                    return;
                }
                loadFailFlag = false;
                showNativeAd();
            }

            @Override
            public void onNativeAdLoadFail(AdError adError) {
                //注意：禁止在此回调中执行广告的加载方法进行重试，否则会引起很多无用请求且可能会导致应用卡顿
                //AdError，请参考 https://docs.takuad.com/#/zh-cn/android/android_doc/android_test?id=aderror
                Log.i(TAG, "onNativeAdLoadFail:" + adError.getFullErrorInfo());
            }
        });
        //发起广告请求
        atNative.makeAdRequest();
    }

    ATNativePrepareInfo nativePrepareInfo;

    public void showAd() {
        if (atNative == null) {
            return;
        }
        if (!atNative.checkAdStatus().isReady()) {
            loadFailFlag = true;
            atNative.makeAdRequest();
            return;
        }
        showNativeAd();
    }

    private void showNativeAd() {
        NativeAd nativeAd = atNative.getNativeAd();
        //开发者可以在调用getNativeAd后直接使用ATNative#makeAdRequest发起预加载下一次的广告
        atNative.makeAdRequest();
        if (nativeAd != null) {
            if (mNativeAd != null) {
                mNativeAd.destory();
            }

            mNativeAd = nativeAd;
            mNativeAd.setAdRevenueListener(new AdRevenueListenerImpl(context,token, TaKuAdPositionEnum.AD_NATIVE_POSITION.getKey(), "2"));
            if (!nativeAd.isNativeExpress()) {
                //自渲染
                nativePrepareInfo = new ATNativePrepareInfo();
            } else {
                //模板渲染
                nativeAd.renderAdContainer(mATNativeAdView, null);
            }
            nativeAd.prepare(mATNativeAdView, nativePrepareInfo);
        }
    }

    public void setmATNativeAdView(ATNativeAdView mATNativeAdView) {
        this.mATNativeAdView = mATNativeAdView;
    }

    public void setToken(String token) {
        this.token = token;
    }


}
